﻿<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            margin: 100px auto 0;
        }

        .wrap h1 {
            text-align: center;
        }

        .wrap div {
            width: 400px;
            height: 300px;
            background: pink;
            font-size: 30px;
            text-align: center;
            line-height: 300px;
        }
    </style>
    <script src="../jquery-1.11.1.js"></script>
    <script>
        jQuery(document).ready(function($) {
            let colors = {65: 'red', 66: 'green', 67: 'blue'}
            $('body').keydown(function(e) {
                $('#keyCodeSpan').html(e.keyCode);
                if (colors.hasOwnProperty(e.keyCode)) {
                    $('#bgChange').css('background', colors[e.keyCode]);
                } else {
                    $('#bgChange').css('background', '');
                }
            });
        });
    </script>
</head>
<body>

    <div class="wrap">
        <h1>按键改变颜色</h1>
        <div id="bgChange">
            keyCode为：
            <span id="keyCodeSpan"></span>
        </div>
    </div>

</body>
</html>
